const { GraphQLScalarType } = require('graphql');
const { Kind } = require('graphql/language');
const dayjs = require('dayjs');

module.exports = new GraphQLScalarType({
  name: 'Date',
  description: '自定义时间类型',
  // 客户端输入
  parseValue(value) {
    return new Date(value);
  },
  // 打印给客户端
  serialize(value) {
    return dayjs(value).format('YYYY-MM-DD HH:mm:ss');
  },
  // 检查类型
  parseLiteral(ast) {
    if (ast.kind === Kind.INT) {
      return parseInt(ast.value, 10);
    }
    return null;
  },
});